package cn.tedu.weibo.controller;

import cn.tedu.weibo.mapper.CommentMapper;
import cn.tedu.weibo.vo.Comment;
import cn.tedu.weibo.vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
public class CommentController {

    @Autowired
    CommentMapper mapper;

    @RequestMapping("/comment/insert")
    public int insert(Comment comment, HttpSession session){
        User u = (User) session.getAttribute("u");
        if (u == null){
            return 2; //未登录
        }
        comment.setAuthor(u.getNick());
        comment.setUserId(u.getId());
        comment.setCreated(new Date());
        mapper.insert(comment);
        return 1;
    }

    @RequestMapping("/comment/select")
    public List<Comment> comments(int id){
        return mapper.selectByWeiboId(id);
    }

    @RequestMapping("/comment/delete")
    public void delete(int id){
        mapper.delete(id);
    }
}
